import Vue from 'vue'
import Vuex from 'vuex'
import ElementUI from 'element-ui'
Vue.use(Vuex)

var defaultInfo = {
    "avatar": "http://00-upload.gayligayqi.com/blog/avatar/default.jpg",
    "backImg": "http://00-upload.gayligayqi.com/blog/headback/default.png",
    "nickName": "未登录",
    "perSign": "",
    "sex": 1,
    "vipLevel": 0,
    "bigV": 0,
    "islogin": false,
    "canPublish": 0,
    "session_id": ""
}
const actions = {
    SOCKET_push_event(state,data){
        //在这里可以作为消息接收
        ElementUI.Notification({
            onClick: function(){
                //消息点击事件
                state.state.showMessageBox=true;
                state.state.choseChatBox=data.user_vo.id;
            },
            dangerouslyUseHTMLString: true,
            //这里存放消息
            message: '<div><img style="width:40px; height:40px;border-radius: 100%;vertical-align: middle;" src="'+data.user_vo.avatar+'"> '+data.user_vo.nickName+'：</div><div style="text-indent: 4em;font-size: 12px;">'+data.content+'</div>'
        });
    }
}
const mutations = {
    setLoginInfo: (state, loginData) => {
        state.loginData = loginData
        // 把登录的用户的名保存到localStorage中，防止页面刷新，导致vuex重新启动，用户名就成为初始值（初始值为空）的情况
        sessionStorage.setItem('loginData', loginData)
    }
}
const state = {
    choseChatBox:0,
    showMessageBox:false,
    loginData: sessionStorage.getItem('loginData') === null ? JSON.stringify(defaultInfo) : sessionStorage.getItem('loginData')
}
// getters 只会依赖 state 中的成员去更新
const getters = {
    loginData: (state) => JSON.parse(state.loginData)
}

const store = new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})
export default store 